<?php

namespace Celebrio;

use Nette\Object;
use Nette\Environment;
use Nette\Config\Config;

class ShadowReader extends Object {

    /**
     * @var Config
     */
    private $shadow_config;

    // sha1(password.celebrity) from shadow.ini
    const PASSWORD = '32d04dd11e678f085c2e8cc825592ad8475d3d9e';

    /**
     * The constructor extracts the configuration from shadow.ini file
     * and stores it in private property $shadow_config. It can be accessed
     * by the public methods below.
     */
    public function __construct() {

        if ($this->shadow_config === null) {
            $file = Environment::expand("%appDir%") . '/shadow.ini';
            $env_name = Environment::getName(); // for example console, development, ...
            $this->shadow_config = Config::fromFile($file, $env_name);
        }

    }

    protected function getFromConfig($group, $key = null) {
        $group_config = $this->shadow_config->get($group);
        if ($key === null) {
            return $group_config;
        } else if ($group_config instanceof Config) {
            $value = $group_config->get($key);
            if(!empty($value)) {
               return $value; 
            }
        } 
        throw new \InvalidArgumentException("There is not '$key' entry in configuration group '$group' (mode " . Environment::getName() . ").");

    }

    public function getPassword($key = null) {
        return $this->getFromConfig("password", $key);
    }

    public function getOAuth($key = null) {
        return $this->getFromConfig("oauth", $key);
    }
}
